#!/bin/bash
line="====================================================================="
DATE=`date +_%m_%d_%H_%M`
np=1
bs_list="4"
nplist="24"
segement_list="1 8 16 32 64 128"
numtarget_list="5"
client_list="4 5"

#设置磁盘sector
set_sector()
{
#ip_list="25.8.58.168 25.8.58.169 25.8.58.171 25.8.58.172 25.8.58.173"
ip_list="89.72.58.172"
for ip in $ip_list
  do
    echo $line
    if [ "$1" == "4" ];then
       sector=512
    else
       sector=1024
    fi
    echo "--- set $ip disk sector as $sector ---"
    echo $line
    ssh $ip sh /root/set_sector.sh $sector
  done
}
#设置chunksize
set_chunksize()
{
  bs=$1
  if [ $bs == "4" ]; then
    chunksize="512k"
    echo $chunksize
  else
    chunksize="1024k"
  fi
  echo $line
  echo "--- setpattern as $chunksize ---"
  echo $line
  orcafs-ctl --setpattern --chunksize=$chunksize  /mnt/orcafs/172/
}
#设置numtarget
set_numtarget()
{
  num=$1
  echo $line
  echo "--- setpattern numtarget as $num ---"
  echo $line
  orcafs-ctl --setpattern --numtargets=$num  /mnt/orcafs/172/
  orcafs-ctl --getentryinfo /mnt/orcafs/172
}

#根据单节点线程数修改hostfile中的slots
modify_hostfile()
{
  np=$1

  echo $line
  echo "--- modify hostfile slots as $np ---"
  echo $line

  sed -i "s/slots=.*/slots=$np/g" /root/hostfile
  #cat /root/hostfile
  echo $line
  echo "--- copy hostfile to another node ---"
  echo $line
  sh  bench_scp.sh /root/hostfile /root/hostfile
}
#根据bs大小修改np数
modify_np()
{
  block_size=$1
  rw_type=$2
  if [ $block_size == "4" ]; then
    np=64
  elif [[ $block_size == "1024" ]] && [[ $rw_type == "_wrz" ]];then
    np=90
  elif [[ $block_size == "1024" ]] && [[ $rw_type == "_wz" ]];then
    np=12
  elif [[ $block_size == "1024" ]] && [[ $rw_type == "_wr" ]];then
    np=90
  elif [[ $block_size == "1024" ]] && [[ $rw_type == "_w" ]];then
    np=12
  fi
}
correction_para()
{
  if [ "$1" == "_wr" ]; then
      rw_mod="-w -r"
  elif [  "$1" == "_wrz" ];then
      rw_mod="-w -r -z"
  elif [  "$1" == "_wz" ];then
      rw_mod=" -w -z "
  else
      rw_mod=" -w"
  fi
}
blocksize_set_by_rwmod()
{
  blocksize=$1
  if [ $blocksize == 4 ];then
     rw_mod="-w -r -z"
     nplist="24"
  elif [ $blocksize == 1024 ];then
     rw_mod="-w -r"
     nplist="12 16 24 32 64"
  fi
}
main()
{
  #mkdir -p  /mnt/orcafs/172
  for bs in $bs_list;  #4 1024
    do
    #设置4K为随机读写,1024K为顺序读写
    blocksize_set_by_rwmod $bs
    block_size=$bs"k"
    echo -e "\033[32m$line\033[0m"
    echo $block_size"_"$rw_mod
    echo -e "\033[32m$line\033[0m"
    #设置磁盘块大小(4k为512k 1024k为1024k)
    set_sector $bs
    sleep 1
    #修改文件目录chunksize与磁盘块大小一致
    set_chunksize $bs


    for np in $nplist #12 16 24 32 64  (24)
      do
        for num in $numtarget_list # 1 5 10  (10)
          do
          #设置numtarget
          set_numtarget $num
          modify_hostfile $np #修改hostfile中的slots与np对应
          for client in $client_list # 1 2 3 4 5 (5)
            do
            multi_np=$[${np}*${client}] #设置mpirun总np数
            ior_cmd="mpirun --allow-run-as-root -np $multi_np -hostfile /root/hostfile -wd /mnt/orcafs/172 ior -v -a POSIX -g -e -b 64m -s 16 -F -t ${block_size} -i 1 $rw_mod  --posix.odirect -O summaryFormat=JSON -O summaryFile=/root/${DATE}_${block_size}_${np}np${rw}_16sg_64m_numtaget${num}_${client}client.json"
            echo -e "\033[33m$ior_cmd\033[0m"
            echo -e "\033[32m$line\033[0m"
            ${ior_cmd}
            #mpirun --allow-run-as-root -np $multi_np -wd /mnt/orcafs/172 ior -v -a POSIX -g -e -b 64m -s 16 -F -t ${block_size} -i 1 $rw_mod  --posix.odirect -O summaryFormat=JSON | tee -a /root/${DATE}_${block_size}_${np}np${rw}_16sg_64m_numtaget${num}_${client}client.json
            done
          done
        done
    done

}
main

